//
//  BrowseViewController.m
//  Newproject3
//
//  Created by rimi on 16/9/12.
//  Copyright © 2016年 Liang. All rights reserved.
//

#import "BrowseViewController.h"
#import "BrowseTableViewCell.h"
#import "NetWorking.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "DetailViewController.h"
#import <MJRefresh.h>

@interface BrowseViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSDictionary *_dict;
    NSArray *_arr;
    NSArray *_localArr;
}
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation BrowseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.centerLabel.text = @"新闻头条";
    
    // 进入界面首先进行的是从本地取到本地数据
    _localArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"localData"];
    [self initUi];
//  [self.tableView reloadData];
    
    [self initData];
    [self refreshUi];
}

-(void)refreshUi{
    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshLoad)];
    
    [footer setImages:nil forState:MJRefreshStateRefreshing];
    [footer setImages:nil forState:MJRefreshStatePulling];
    [footer setImages:nil forState:MJRefreshStateRefreshing];
    
    self.tableView.mj_footer = footer;
}

-(void)refreshLoad{
    // 刷新的时候，跟新数据
    [self.tableView.mj_footer endRefreshing];
}

-(void)initUi{
    [self.view addSubview:self.tableView];
}

-(void)initData{
    NSString *URL = @"http://apis.baidu.com/showapi_open_bus/channel_news/search_news";
    
    [[NetWorking shareNetWork] startGetRequestWithUrl:URL paramters:nil success:^(id object) {
    _arr = object[@"showapi_res_body"][@"pagebean"][@"contentlist"];
          // 本地储存
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:_arr forKey:@"localData"];
        NSLog(@"____%@",object);
        // 同时进行网络请求，然后同步本地储存的数据
        if (_arr != nil) {
            _localArr = _arr;
            [self.tableView reloadData];
          
        }

    } failed:^(id object) {
      
    }];
}

#pragma mark -- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _localArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BrowseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = _localArr[indexPath.row][@"title"];
    if (_localArr[indexPath.row][@"desc"]) {
         cell.detailLabel.text = _localArr[indexPath.row][@"desc"];
    }else{
        cell.detailLabel.text = nil;
    }
   
    if ([_localArr[indexPath.row][@"imageurls"] count] == 0) {
        cell.leftImage.image = [UIImage imageNamed:@"loaded.jpg"];
    }else{
      [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:_localArr[indexPath.row][@"imageurls"][0][@"url"]] placeholderImage:nil];
    }
    
    return cell;
}

#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return khieght/5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *vc = [[DetailViewController alloc]init];
    vc.sendArray = _localArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- getter

-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kwidth, khieght-64-44) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[BrowseTableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

@end
